﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "CommandBase.h"

#include "IUiDocumentViewManager.h"
#include "IUiDocument.h"
#include "IDocument.h"
#include "ISelection.h"
#include "IParameterDefinitionLibrary.h"
#include "UniIdentity.h"

#include "GDDrawing/ICurrentVertexMarkRenderUIService.h"
#include "GDFoundation/Command.h"

using namespace gcmp;
namespace Sample
{
    namespace CommandId
    {
        constexpr auto CurrentVertexMarkRender = L"Sample.CommandId.CurrentVertexMarkRender";
    }

    class CurrentVertexMarkRenderCommand :public  gcmp::CommandBase
    {
    public:
        CurrentVertexMarkRenderCommand()
            :CommandBase(Sample::CommandId::CurrentVertexMarkRender) {};
    private:
        bool IsEnabled() const override;
        bool IsVisible() const override;
        bool ShouldPopExistingActionsBeforeExecution() const override { return false; }
        bool ShouldClearSelectionBeforeExecution() const override { return false; }
        gcmp::OwnerPtr<gcmp::IAction> ExecuteCommand(const gcmp::CommandParameters& cmdParams) override;
    private:
        gcmp::IDocument* GetDocument() const;
    };

    bool CurrentVertexMarkRenderCommand::IsEnabled() const
    {
        gcmp::IDocument* pDocument = GetDocument();
        return pDocument != nullptr;
    }

    bool CurrentVertexMarkRenderCommand::IsVisible() const
    {
        gcmp::IDocument* pDocument = GetDocument();
        return pDocument != nullptr;
    }

    gcmp::OwnerPtr<gcmp::IAction> CurrentVertexMarkRenderCommand::ExecuteCommand(const gcmp::CommandParameters& cmdParams)
    {
        gcmp::IDocument* pDocument = GetDocument();
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDocument, L"断言`pDocument`失败",L"GDMPLab",L"2024-03-30");

        //获取当前选择集中的Element
        gcmp::ISelection* pSelection = ISelection::Get();
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pSelection, L"断言`pSelection`失败",L"GDMPLab",L"2024-03-30");
        const std::unordered_set<ElementId, ElementIdHash>&  elementIds = pSelection->GetSelectedElements();
        if (elementIds.empty()) {
            return nullptr;
        }
        gcmp::ElementId elementId = *elementIds.begin();

        //查找当前顶点参数的ID等信息
        gcmp::IParameterDefinitionLibrary* pParameterDefinitionLibrary = IParameterDefinitionLibrary::Get(pDocument);
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pParameterDefinitionLibrary, L"断言`pParameterDefinitionLibrary`失败",L"GDMPLab",L"2024-03-30");

        gcmp::UniIdentity uid = gcmp::UniIdentity::Create(
            //当前顶点参数的GUID
            gcmp::GuidUtils::FromString(L"{F1FAA54E-7708-420A-A591-17321F57CA57}"),
            L"");
        int parameterId = pParameterDefinitionLibrary->GetParameterDefinitionIdByUid(uid);

        //注意，该接口应当在属性面板中调用，这里为了演示，采用命令的方式
        //构造了接口所需参数
        gdmp::ICurrentVertexMarkRenderUIService::OnFocusIn(
            pDocument,
            parameterId,
            elementId
        );
        return nullptr;
    }

    gcmp::IDocument* CurrentVertexMarkRenderCommand::GetDocument() const
    {
        gcmp::IUiDocumentViewManager* pUiDocumentViewManager = IUiDocumentViewManager::Get();;
        if (pUiDocumentViewManager) {
            gcmp::IUiDocument* pUiDocument = pUiDocumentViewManager->GetCurrentUiDocument();
            if (pUiDocument) {
                gcmp::IDocument* pDocument = pUiDocument->GetDbDocument();
                if (pDocument) {
                    if (!pDocument->IsFamilyDocument()) {
                        return pDocument;
                    }
                }
            }
        }
        return nullptr;
    }

    namespace
    {
        //仅作为示例展示使用，注册命令,当析构时移除注册
        gdmp::Command stub{ NEW_AS_OWNER_PTR(CurrentVertexMarkRenderCommand) };
    }
}
